package com.lill.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.product.dao.IBaseRoyaltyDao;
import com.lill.product.dao.IProductCarDao;
import com.lill.product.dao.ISaleRoyaltyDao;
import com.lill.product.model.BaseRoyaltyModel;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.ISaleRoyaltyService;

/**
 * 销售提成值Service实现类
 *
 * @author lill
 * @date 2021-09-06 20:23:17
 */
@Service("com.lill.product.service.impl.SaleRoyaltyServiceImpl")
public class SaleRoyaltyServiceImpl implements ISaleRoyaltyService {
	
    @Autowired
    private ISaleRoyaltyDao saleRoyaltyDao;
    @Autowired
    private IBaseRoyaltyDao baseRoyaltyDao;
    @Autowired
    private IProductCarDao productCarDao;

    public Map<String, Object> queryObject(String id) {
    	Map<String, Object> royaltyMap=saleRoyaltyDao.queryObject(id);
    	if(null!=royaltyMap){
    		royaltyMap.put("productId", royaltyMap.get("product_id"));
    	}
        return royaltyMap;
    }

    public List<Map<String, Object>> queryList(Map<String, Object> map) {
    	Map<String, Object> mapCol=new HashMap<String, Object>();
    	mapCol.put("companyId", map.get("companyId"));
    	if(map.containsKey("branchId")){
    		mapCol.put("branchId", map.get("branchId"));
    	}else{
    		mapCol.put("branchIdIsNull", map.get("branchIdIsNull"));
    	}
    	List<BaseRoyaltyModel> baseDiscountList=baseRoyaltyDao.queryList(mapCol);
    	if(CollectionUtils.isNotEmpty(baseDiscountList)){
    		List<String> colNameList=new ArrayList<String>();
    		for (BaseRoyaltyModel baseDiscount : baseDiscountList) {
    			colNameList.add(baseDiscount.getMapName());
			}
    		map.put("colNameList", colNameList);
    	}
    	List<Map<String, Object>> mapList=saleRoyaltyDao.queryList(map);
    	if(CollectionUtils.isNotEmpty(mapList)){
    		Map<Object, ProductCarModel> carModelMap=new HashMap<Object, ProductCarModel>();
    		for (Map<String, Object> mapRoyalty : mapList) {
    			if(carModelMap.containsKey(mapRoyalty.get("product_id"))){
    				mapRoyalty.put("carModel", carModelMap.get(mapRoyalty.get("product_id")));
    			}else{
    				ProductCarModel carModel=productCarDao.queryObject(mapRoyalty.get("product_id"));
    				mapRoyalty.put("carModel", carModel);
    				carModelMap.put(mapRoyalty.get("product_id"), carModel);
    			}
			}
    	}
        return mapList;
    }

    public int queryTotal(Map<String, Object> map) {
        return saleRoyaltyDao.queryTotal(map);
    }

    public int save(Map<String, Object> royaltyMap) {
    	int num=saleRoyaltyDao.queryTotal(royaltyMap);
    	if(royaltyMap.containsKey("productId")){
    		royaltyMap.put("product_id", royaltyMap.get("productId"));
    		royaltyMap.remove("productId");
    	}
    	if(num>0){
    		return saleRoyaltyDao.update(royaltyMap);
    	}else{
    		royaltyMap.put("id", IdUtil.createIdbyUUID());
    		return saleRoyaltyDao.save(royaltyMap);
    	}
    }

}
